﻿using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiMeiJiaLibModel.Business;
using YiMeiJiaLibModel.Business.Dto;
using YiMeiJiaLibModel.Enums;
using YiMeiJiaService.Assembly;
using YiMeiJiaService.IService;


namespace YiMeiJiaApp.Pages.Home.Mall
{
    public partial class Categroy
    {
        #region 服务组件

        [Inject] private IAcccount _account { get; set; }
        [Inject] private IGoods _goods { get; set; }

        [Inject] protected NavigationManager Navigation { get; set; }


        [Inject] protected IJSRuntime JSRuntime { get; set; }
        #endregion

        /// <summary>
        /// 所有分类
        /// </summary>
        public List<WxGoodCategoryDto> _categorys { get; set; }

        /// <summary>
        /// 选中的子分类
        /// </summary>

        public List<WxGoodCategoryChild> _selectChild { get; set; }

        public int selectChildId = 0;

        #region 绑定数据


        #endregion
        /// <summary>
        /// 加载事件
        /// </summary>
        /// <returns></returns>
        protected override async Task OnInitializedAsync()
        {

            try
            {
                string msg = "all";
                _categorys = await _goods.GetAllCategories(msg); // 得到所有分类
                if (_categorys != null && _categorys.Count > 0)
                {
                    foreach (var item in _categorys)
                    {
                        if (item.child != null && item.child.Any())
                        {
                            _selectChild = item.child;
                            selectChildId = item.id;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            await base.OnInitializedAsync();
        }

        /// <summary>
        /// 绑定和更新ID
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public async Task BindCategorys(int categoryId)
        {

            EvalJs.ExJs().EvalVoid(JSRuntime, $"selectCategory({categoryId})");
            var sel_obj = _categorys.Where(x => x.id == categoryId).FirstOrDefault();  
            if (sel_obj != null)
            {
                _selectChild = sel_obj.child;
            }
            EvalJs.ExJs().EvalVoid(JSRuntime, $"selectCategory({categoryId})");
        }

        /// <summary>
        /// 跳转到具体分类页面
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public async  Task JumpToCategoryLis(int categoryId)
        {
            Navigation.NavigateTo($"/Home/Mall/List?categoryId={categoryId}");
        }
    }
}
